package timer;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;

/**
 * @ClassName CancelTest
 * @Description TODO
 * @Author Wxg
 * @Date 2018/11/13 15:57
 * @Version 1.0
 */
public class CancelTest {

    public static void main(String[] args) throws InterruptedException {
        Timer timer = new Timer();

        MyTimerTask task1 = new MyTimerTask("task1");
        MyTimerTask task2 = new MyTimerTask("task2");

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startTime = new Date();
        System.out.println("start time is : " + sdf.format(startTime));

        timer.schedule(task1, 3000, 2000);
        timer.schedule(task2, 1000, 2000);

        Thread.sleep(5000);

        System.out.println("cancel time is : " + sdf.format(new Date()));

        timer.cancel();
        System.out.println("Tasks all canceled!");
    }
}
